<html>
<head>
  <meta HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=ISO-8859-1">
  <title>svmlight.m</title>
<link rel="stylesheet" type="text/css" href="../../m-syntax.css">
</head>
<body>
<code>
<span class=defun_kw>function</span>&nbsp;<span class=defun_out>model</span>=<span class=defun_name>svmlight</span>(<span class=defun_in>data,options</span>)<br>
<span class=h1>%&nbsp;SVMLIGHT&nbsp;Interface&nbsp;to&nbsp;SVM^{light}&nbsp;software.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Synopsis:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svmlight(data)</span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;=&nbsp;svmlight(data,options)</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Description:</span></span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;serves&nbsp;as&nbsp;an&nbsp;interface&nbsp;between&nbsp;Matlab&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;and&nbsp;SVM^{light}&nbsp;(Version:&nbsp;5.00)&nbsp;optimizer&nbsp;which&nbsp;trains&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;the&nbsp;Support&nbsp;Vector&nbsp;Machines&nbsp;classifier.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;executable&nbsp;file&nbsp;'svm_learn'&nbsp;must&nbsp;be&nbsp;in&nbsp;the&nbsp;path.&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;The&nbsp;SVM^{light}&nbsp;software&nbsp;can&nbsp;be&nbsp;downloaded&nbsp;from:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;http://svmlight.joachims.org/</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;&nbsp;This&nbsp;function&nbsp;creates&nbsp;temporary&nbsp;files&nbsp;'tmp_alphaXX.txt',&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;'tmp_examplesXX.txt',&nbsp;'tmp_modelXX.txt'&nbsp;and&nbsp;'tmp_verbXX.txt'&nbsp;for&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;comunication&nbsp;with&nbsp;the&nbsp;SVM^{light}&nbsp;software.&nbsp;The&nbsp;XX=datestr(now)</span><br>
<span class=help>%&nbsp;&nbsp;is&nbsp;string&nbsp;consisting&nbsp;of&nbsp;current&nbsp;date&nbsp;and&nbsp;time.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;<span class=help_field>Input:</span></span><br>
<span class=help>%&nbsp;&nbsp;data&nbsp;[struct]&nbsp;Labeled&nbsp;binary&nbsp;data:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.X&nbsp;[dim&nbsp;x&nbsp;num_data]&nbsp;Training&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.y&nbsp;[1&nbsp;x&nbsp;num_data]&nbsp;Labels&nbsp;of&nbsp;training&nbsp;data&nbsp;(1&nbsp;or&nbsp;2).</span><br>
<span class=help>%&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;options&nbsp;[struct]&nbsp;Control&nbsp;parameters:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.ker&nbsp;[string]&nbsp;Kernel&nbsp;identifier:&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'linear'&nbsp;(default),'rbf'&nbsp;and&nbsp;'poly'.&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.arg&nbsp;[1x1]&nbsp;Kernel&nbsp;argument&nbsp;(default&nbsp;[]).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.C&nbsp;[1x1]&nbsp;SVM&nbsp;regularization&nbsp;constant&nbsp;(default&nbsp;C=inf).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.mC&nbsp;[1x1]&nbsp;if&nbsp;mC&nbsp;is&nbsp;given&nbsp;then&nbsp;C&nbsp;is&nbsp;set&nbsp;to&nbsp;mC/length(data.y).&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.j&nbsp;[1x1]&nbsp;Cost-factor,&nbsp;by&nbsp;which&nbsp;training&nbsp;errors&nbsp;on&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;positive&nbsp;examples&nbsp;outweight&nbsp;errors&nbsp;on&nbsp;negative&nbsp;examples&nbsp;(default&nbsp;1).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.eps&nbsp;[1x1]&nbsp;Tolerance&nbsp;of&nbsp;KKT-conditions&nbsp;(default&nbsp;eps=0.001).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;if&nbsp;1&nbsp;(default)&nbsp;then&nbsp;finds&nbsp;w'*x&nbsp;+b&nbsp;else&nbsp;b&nbsp;=&nbsp;0;</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.keep_files&nbsp;[1x1]&nbsp;If&nbsp;==1&nbsp;then&nbsp;keeps&nbsp;temporary&nbsp;files&nbsp;otherwise</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;erase&nbsp;them.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.svm_command&nbsp;[string]&nbsp;Path&nbsp;to&nbsp;SVM^{light}&nbsp;solver&nbsp;(default&nbsp;"svm_learn")</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Output:</span></span><br>
<span class=help>%&nbsp;&nbsp;model&nbsp;[struct]&nbsp;Binary&nbsp;SVM&nbsp;classifier:</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.Alpha&nbsp;[nsv&nbsp;x&nbsp;1]&nbsp;Weights&nbsp;of&nbsp;support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.b&nbsp;[1x1]&nbsp;Bias&nbsp;of&nbsp;decision&nbsp;function.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.X&nbsp;[dim&nbsp;x&nbsp;nsv]&nbsp;Support&nbsp;vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.sv.inx&nbsp;[1&nbsp;x&nbsp;nsv]&nbsp;Indices&nbsp;of&nbsp;SVs&nbsp;(model.sv.X&nbsp;=&nbsp;data.X(:,inx)).</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.nsv&nbsp;[int]&nbsp;Number&nbsp;of&nbsp;Support&nbsp;Vectors.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.kercnt&nbsp;[int]&nbsp;Number&nbsp;of&nbsp;kernel&nbsp;evaluations&nbsp;used&nbsp;by&nbsp;the&nbsp;SVM^{light}.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.trnerr&nbsp;[real]&nbsp;Classification&nbsp;error&nbsp;on&nbsp;training&nbsp;data.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.margin&nbsp;[real]&nbsp;Margin&nbsp;of&nbsp;found&nbsp;classifier.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.options&nbsp;[struct]&nbsp;Copy&nbsp;of&nbsp;used&nbsp;options.</span><br>
<span class=help>%&nbsp;&nbsp;&nbsp;.cputime&nbsp;[real]&nbsp;Used&nbsp;CPU&nbsp;time&nbsp;in&nbsp;seconds.</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;<span class=help_field>Example:</span></span><br>
<span class=help>%&nbsp;&nbsp;data=load('riply_trn');&nbsp;&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;model=svmlight(data,struct('ker','rbf','C',10,'arg',1))</span><br>
<span class=help>%&nbsp;&nbsp;figure;&nbsp;ppatterns(data);&nbsp;psvm(model);</span><br>
<span class=help>%</span><br>
<span class=help>%&nbsp;See&nbsp;also&nbsp;</span><br>
<span class=help>%&nbsp;&nbsp;SVMCLASS,&nbsp;XY2SVMLIGHT.</span><br>
<span class=help>%</span><br>
<hr>
<span class=help1>%&nbsp;<span class=help1_field>About:</span>&nbsp;Statistical&nbsp;Pattern&nbsp;Recognition&nbsp;Toolbox</span><br>
<span class=help1>%&nbsp;(C)&nbsp;1999-2003,&nbsp;Written&nbsp;by&nbsp;Vojtech&nbsp;Franc&nbsp;and&nbsp;Vaclav&nbsp;Hlavac</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.cvut.cz"&gt;Czech&nbsp;Technical&nbsp;University&nbsp;Prague&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://www.feld.cvut.cz"&gt;Faculty&nbsp;of&nbsp;Electrical&nbsp;Engineering&lt;/a&gt;</span><br>
<span class=help1>%&nbsp;&lt;a&nbsp;href="http://cmp.felk.cvut.cz"&gt;Center&nbsp;for&nbsp;Machine&nbsp;Perception&lt;/a&gt;</span><br>
<br>
<br>
<span class=help1>%&nbsp;<span class=help1_field>Modifications:</span></span><br>
<span class=help1>%&nbsp;09-sep-2007,&nbsp;VF,&nbsp;-b&nbsp;option&nbsp;added</span><br>
<span class=help1>%&nbsp;21-may-2007,&nbsp;VF,&nbsp;-q&nbsp;42&nbsp;(size&nbsp;of&nbsp;QP&nbsp;subproblem)&nbsp;added&nbsp;based&nbsp;on&nbsp;Soeren's&nbsp;suggestion</span><br>
<span class=help1>%&nbsp;20-nov-2006,&nbsp;VF,&nbsp;added&nbsp;optional&nbsp;parameter&nbsp;mC</span><br>
<span class=help1>%&nbsp;10-oct-2006,&nbsp;VF,&nbsp;"svm_command"&nbsp;option&nbsp;added</span><br>
<span class=help1>%&nbsp;09-feb-2006,&nbsp;VF,&nbsp;added&nbsp;date_str(findstr(date_str,':'))&nbsp;=&nbsp;'.';&nbsp;based&nbsp;on</span><br>
<span class=help1>%&nbsp;&nbsp;&nbsp;M.Urban&nbsp;comment.</span><br>
<span class=help1>%&nbsp;16-may-2004,&nbsp;VF</span><br>
<span class=help1>%&nbsp;15-jan-2004,&nbsp;VF,&nbsp;handling&nbsp;argument&nbsp;of&nbsp;poly&nbsp;kernel&nbsp;repared</span><br>
<span class=help1>%&nbsp;10-oct-2003,&nbsp;VF,&nbsp;computation&nbsp;of&nbsp;lin&nbsp;model&nbsp;added</span><br>
<span class=help1>%&nbsp;29-aug-2003,&nbsp;VF,&nbsp;seconds&nbsp;are&nbsp;added&nbsp;to&nbsp;the&nbsp;name&nbsp;of&nbsp;temporary&nbsp;files</span><br>
<span class=help1>%&nbsp;12-may-2003,&nbsp;VF,&nbsp;1st&nbsp;3&nbsp;lines&nbsp;of&nbsp;verb_file&nbsp;are&nbsp;skiped</span><br>
<span class=help1>%&nbsp;31-jan-2003,&nbsp;VF,&nbsp;added&nbsp;option&nbsp;'j'&nbsp;</span><br>
<span class=help1>%&nbsp;28-Jan-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;20-jan-2003,&nbsp;VF,&nbsp;temporary&nbsp;files&nbsp;are&nbsp;unique&nbsp;and&nbsp;are&nbsp;deleted&nbsp;at&nbsp;the&nbsp;end</span><br>
<span class=help1>%&nbsp;14-Jan-2003,&nbsp;VF</span><br>
<span class=help1>%&nbsp;26-sep-2002,&nbsp;VF</span><br>
<span class=help1>%&nbsp;3-Jun-2002,&nbsp;V.Franc</span><br>
<br>
<hr>
tic;<br>
<br>
data=c2s(data);<br>
<br>
<span class=comment>%&nbsp;gets&nbsp;current&nbsp;date&nbsp;and&nbsp;time</span><br>
date_str=datestr(now);<br>
date_str(findstr(date_str,<span class=quotes>'&nbsp;'</span>))&nbsp;=&nbsp;<span class=quotes>'-'</span>;<br>
date_str(findstr(date_str,<span class=quotes>':'</span>))&nbsp;=&nbsp;<span class=quotes>'.'</span>;<br>
sec=clock;<br>
date_str&nbsp;=&nbsp;[date_str&nbsp;<span class=quotes>'-'</span>&nbsp;num2str(sec(<span class=keyword>end</span>))];<br>
<br>
<br>
<span class=comment>%&nbsp;names&nbsp;of&nbsp;temporary&nbsp;files&nbsp;</span><br>
examples_file&nbsp;=&nbsp;[<span class=quotes>'tmp_examples'</span>&nbsp;date_str&nbsp;<span class=quotes>'.txt'</span>];<br>
model_file&nbsp;=&nbsp;[<span class=quotes>'tmp_model'</span>&nbsp;date_str&nbsp;<span class=quotes>'.txt'</span>];<br>
verb_file&nbsp;=&nbsp;[<span class=quotes>'tmp_verb'</span>&nbsp;date_str&nbsp;<span class=quotes>'.txt'</span>];<br>
alpha_file&nbsp;=&nbsp;[<span class=quotes>'tmp_alpha'</span>&nbsp;date_str&nbsp;<span class=quotes>'.txt'</span>];<br>
<br>
<span class=comment>%&nbsp;make&nbsp;model</span><br>
model.name&nbsp;=&nbsp;<span class=quotes>'SVM&nbsp;classifier'</span>;<br>
<br>
<span class=comment>%&nbsp;--&nbsp;Process&nbsp;input&nbsp;arguments&nbsp;--------------------------</span><br>
<span class=keyword>if</span>&nbsp;<span class=stack>nargin</span>&nbsp;&lt;&nbsp;2,&nbsp;options&nbsp;=&nbsp;[];&nbsp;<span class=keyword>else</span>&nbsp;options=c2s(options);&nbsp;<span class=keyword>end</span><br>
<br>
<span class=keyword>if</span>&nbsp;isfield(options,<span class=quotes>'mC'</span>),&nbsp;options.C&nbsp;=&nbsp;options.mC/length(data.y);&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'ker'</span>),&nbsp;options.ker&nbsp;=&nbsp;<span class=quotes>'linear'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'arg'</span>),&nbsp;options.arg&nbsp;=&nbsp;<span class=quotes>'[]'</span>;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'C'</span>),&nbsp;options.C&nbsp;=&nbsp;inf;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'eps'</span>),&nbsp;options.eps&nbsp;=&nbsp;0.001;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'keep_files'</span>),&nbsp;options.keep_files&nbsp;=&nbsp;0;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'j'</span>),&nbsp;options.j&nbsp;=&nbsp;1;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'b'</span>),&nbsp;options.b&nbsp;=&nbsp;1;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>if</span>&nbsp;~isfield(options,<span class=quotes>'svm_command'</span>),&nbsp;options.svm_command&nbsp;=&nbsp;<span class=quotes>'svm_learn'</span>;&nbsp;<span class=keyword>end</span><br>
<br>
<span class=comment>%&nbsp;gets&nbsp;data&nbsp;dimensions</span><br>
[dim,num_data&nbsp;]&nbsp;=&nbsp;size(data);<br>
<br>
<span class=comment>%--------------------------------</span><br>
<span class=keyword>switch</span>&nbsp;options.ker<br>
&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'linear'</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;ker=<span class=quotes>'-t&nbsp;0'</span>;<br>
&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'rbf'</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;ker=[<span class=quotes>'-t&nbsp;2&nbsp;-g&nbsp;'</span>&nbsp;num2str(1/(2*options.arg^2))];&nbsp;<br>
&nbsp;&nbsp;<span class=label>case</span>&nbsp;<span class=quotes>'poly'</span>&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;length(options.arg)&nbsp;==&nbsp;1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ker=[<span class=quotes>'-t&nbsp;1&nbsp;-r&nbsp;1&nbsp;-s&nbsp;1&nbsp;-d&nbsp;'</span>&nbsp;num2str(options.arg)];&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>else</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ker=[<span class=quotes>'-t&nbsp;1&nbsp;-s&nbsp;1&nbsp;-r&nbsp;'</span>&nbsp;num2str(options.arg(2))&nbsp;<span class=quotes>'&nbsp;-d&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;num2str(options.arg(1))];&nbsp;&nbsp;<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
command=[options.svm_command&nbsp;<span class=quotes>'&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-c&nbsp;'</span>&nbsp;num2str(options.C)&nbsp;<span class=quotes>'&nbsp;'</span>...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ker&nbsp;<span class=quotes>'&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-v&nbsp;1&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-m&nbsp;40&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-q&nbsp;42&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-j&nbsp;'</span>&nbsp;num2str(options.j)&nbsp;<span class=quotes>'&nbsp;'</span>...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-e&nbsp;'</span>&nbsp;num2str(options.eps)&nbsp;<span class=quotes>'&nbsp;'</span>...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-b&nbsp;'</span>&nbsp;num2str(options.b)&nbsp;<span class=quotes>'&nbsp;'</span>&nbsp;...<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=quotes>'-a&nbsp;'</span>&nbsp;alpha_file&nbsp;<span class=quotes>'&nbsp;'</span>&nbsp;examples_file&nbsp;<span class=quotes>'&nbsp;'</span>&nbsp;model_file&nbsp;<span class=quotes>'&nbsp;&gt;&nbsp;'</span>&nbsp;verb_file];<br>
&nbsp;&nbsp;&nbsp;<br>
<span class=comment>%&nbsp;converts&nbsp;data&nbsp;to&nbsp;SVM^light&nbsp;format</span><br>
xy2svmlight(data,examples_file);<br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
<span class=comment>%&nbsp;call&nbsp;svm_learn</span><br>
[a,b]=system(command);&nbsp;&nbsp;&nbsp;<br>
<br>
<span class=comment>%&nbsp;parses&nbsp;model&nbsp;file</span><br>
checkfile(model_file);&nbsp;[lines]=textread(model_file,<span class=quotes>'%s'</span>);<br>
<span class=keyword>for</span>&nbsp;i=1:size(lines,1)<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;strcmpi(&nbsp;lines(i),&nbsp;<span class=quotes>'threshold'</span>&nbsp;)==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;model.b=-str2num(&nbsp;lines{i-2});<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class=jump>break</span>;<br>
&nbsp;&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
&nbsp;&nbsp;&nbsp;&nbsp;<br>
checkfile(alpha_file);&nbsp;model.Alpha=textread(alpha_file,<span class=quotes>'%f'</span>);<br>
model.Alpha=model.Alpha(:);<br>
model.Alpha(find(data.y==2))&nbsp;=&nbsp;-model.Alpha(find(data.y==2));<br>
<br>
checkfile(verb_file);<br>
[lines]=textread(verb_file,<span class=quotes>'%s'</span>,-1,<span class=quotes>'bufsize'</span>,5000000,<span class=quotes>'headerlines'</span>,3);<br>
<span class=keyword>for</span>&nbsp;i=1:size(lines,1)<br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;strcmpi(&nbsp;lines{i},&nbsp;<span class=quotes>'misclassified,'</span>&nbsp;),<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.trnerr=str2num(&nbsp;lines{i-1}(2:<span class=keyword>end</span>));<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.trnerr=model.trnerr/length(model.Alpha);<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;strcmpi(&nbsp;lines(i),&nbsp;<span class=quotes>'vector:'</span>&nbsp;)&nbsp;&&nbsp;strcmpi(&nbsp;lines(i-1),&nbsp;<span class=quotes>'weight'</span>&nbsp;)==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;tmp=str2num(&nbsp;lines{i+1}(5:<span class=keyword>end</span>));<br>
&nbsp;&nbsp;&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;tmp~=0,&nbsp;model.margin=1/tmp;&nbsp;<span class=keyword>else</span>&nbsp;model.margin=[];&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;strcmpi(&nbsp;lines(i),&nbsp;<span class=quotes>'SV:'</span>&nbsp;)==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.nsv=str2num(&nbsp;lines{i+1});<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
&nbsp;&nbsp;<span class=keyword>if</span>&nbsp;strcmpi(&nbsp;lines(i),&nbsp;<span class=quotes>'evaluations:'</span>&nbsp;)==1,<br>
&nbsp;&nbsp;&nbsp;&nbsp;model.kercnt=str2num(&nbsp;lines{i+1});<br>
&nbsp;&nbsp;<span class=keyword>end</span><br>
<span class=keyword>end</span><br>
<br>
model.nsv&nbsp;=&nbsp;length(find(model.Alpha~=0));<br>
<br>
inx=find(model.Alpha);<br>
model.sv.X&nbsp;=&nbsp;data.X(:,inx);<br>
model.sv.y&nbsp;=&nbsp;data.y(inx);<br>
model.sv.inx&nbsp;=&nbsp;inx;<br>
model.Alpha&nbsp;=&nbsp;model.Alpha(inx);<br>
model.Alpha(find(model.sv.y==2))&nbsp;=&nbsp;-model.Alpha(find(model.sv.y==2));<br>
<br>
<span class=keyword>if</span>&nbsp;strcmp(&nbsp;options.ker,&nbsp;<span class=quotes>'linear'</span>),<br>
&nbsp;&nbsp;model.W&nbsp;=&nbsp;model.sv.X&nbsp;*&nbsp;model.Alpha;<br>
<span class=keyword>end</span><br>
<br>
model.options&nbsp;=&nbsp;options;<br>
model.fun&nbsp;=&nbsp;<span class=quotes>'svmclass'</span>;<br>
<br>
<span class=comment>%&nbsp;deletes&nbsp;temporary&nbsp;files</span><br>
<span class=keyword>if</span>&nbsp;options.keep_files&nbsp;==&nbsp;0,<br>
&nbsp;&nbsp;delete(examples_file);<br>
&nbsp;&nbsp;delete(model_file);<br>
&nbsp;&nbsp;delete(verb_file);<br>
&nbsp;&nbsp;delete(alpha_file);<br>
<span class=keyword>end</span><br>
<br>
model.cputime=toc;<br>
<br>
<span class=jump>return</span>;<br>
<br>
<br>
<span class=defun_kw>function</span>&nbsp;<span class=defun_name>checkfile</span>(<span class=defun_in>fname</span>)<br>
<span class=comment>%&nbsp;Check&nbsp;if&nbsp;file&nbsp;of&nbsp;given&nbsp;name&nbsp;exists.&nbsp;If&nbsp;not&nbsp;then&nbsp;prints</span><br>
<span class=comment>%&nbsp;error.</span><br>
<span class=comment>%&nbsp;</span><br>
<br>
attempts=0;<br>
found&nbsp;=&nbsp;<span class=stack>exist</span>(fname);<br>
<span class=keyword>while</span>&nbsp;attempts&nbsp;&lt;&nbsp;5&nbsp;&&&nbsp;~found<br>
&nbsp;&nbsp;found&nbsp;=&nbsp;<span class=stack>exist</span>(fname);<br>
&nbsp;&nbsp;attempts&nbsp;=&nbsp;attempts+1;&nbsp;&nbsp;<br>
<span class=keyword>end</span><br>
<br>
<span class=keyword>if</span>&nbsp;found&nbsp;==&nbsp;0,<br>
&nbsp;&nbsp;<span class=error>error</span>(<span class=quotes>'File&nbsp;%s&nbsp;not&nbsp;found.\n'</span>,&nbsp;fname);<br>
<span class=keyword>end</span><br>
<br>
<span class=jump>return</span>;<br>
<br>
<span class=comment>%EOF</span><br>
<br>
<br>
<br>
</code>
